package com.paic.custserv.web.controller.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.util.LangUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.paic.custserv.common.Constant;
import com.paic.custserv.common.ResultMap;
import com.paic.custserv.common.enums.StatusEnum;
import com.paic.custserv.common.enums.WorkOrderStatus;
import com.paic.custserv.domain.CussWorkOrder;
import com.paic.custserv.domain.Page;
import com.paic.custserv.service.ICussWorkOrderService;
import com.paic.custserv.utils.CommonUtil;
import com.paic.custserv.web.controller.vo.CussWorkOrderQueryVO;

@RequestMapping("manager/workorder")
@Controller
public class ManagerWorderOrderController {
	
	private static final Logger logger = LoggerFactory.getLogger(ManagerWorderOrderController.class);
	
	@Autowired
	private ICussWorkOrderService cussWorkOrderService;
	
	/**
	 * 全部工单
	 */
	@RequestMapping("whole.do")
	public String wholeWorkOrder(Model model) {
		model.addAttribute("optype", "whole");
		return "manager/whole";
	}
	
	/**
	 * 分页查询全部工单
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("whole/query.do")
	public @ResponseBody ResultMap wholeQuery(CussWorkOrderQueryVO vo) {
		try{
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrder(vo));
		}catch(Exception e) {
			logger.error("客户经理分页查询全部工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 待审核工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("audit.do")
	public String auditWorkOrder(Model model) {
		model.addAttribute("optype", "audit");
		return "manager/audit";
	}
	
	/**
	 * 分页查询待审核工单
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("audit/query.do")
	public @ResponseBody ResultMap auditQuery(CussWorkOrderQueryVO vo) {
		try{			
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrderByType(vo, WorkOrderStatus.Audit));
		}catch(Exception e) {
			logger.error("客户经理分页查询待审核工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 已审核工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("approved.do")
	public String approvedWorkOrder(Model model) {
		model.addAttribute("optype", "approved");
		return "manager/approved";
	}
	
	/**
	 * 分页查询已审核工单出错
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("approved/query.do")
	public @ResponseBody ResultMap approvedQuery(CussWorkOrderQueryVO vo) {
		try{
			vo.setMuchWorkOrderStatus(Constant.MUCH_AUDITED_WORK_ORDER_STATUS);
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrderByType(vo, WorkOrderStatus.Approved));		
		}catch(Exception e) {
			logger.error("客户经理分页查询已审核工单出错出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 处理中工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("inhand.do")
	public String inhandWorkOrder(Model model) {
		model.addAttribute("optype", "inhand");
		return "manager/inhand";
	}
	
	/**
	 * 分页查询处理中工单
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("inhand/query.do")	
	public @ResponseBody ResultMap inhankQuery(CussWorkOrderQueryVO vo) {
		try{
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrderByType(vo, WorkOrderStatus.InHand));	
		}catch(Exception e) {
			logger.error("客户经理分页查询处理中工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 待处理工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("pending.do")
	public String pendingWorkOrder(Model model) {
		model.addAttribute("optype", "pending");
		return "manager/inhand";
	}
	
	/**
	 * 分页查询待处理工单
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("pending/query.do")	
	public @ResponseBody ResultMap pendingQuery(CussWorkOrderQueryVO vo) {
		try{
			vo.setMuchWorkOrderStatus(Constant.MUCH_PENDING_WORK_ORDER_STATUS);
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrderByType(vo, WorkOrderStatus.Pending));	
		}catch(Exception e) {
			logger.error("客户经理分页查询待处理工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 已关闭工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("closed.do")
	public String closedWorkOrder(Model model) {
		model.addAttribute("optype", "closed");
		return "manager/closed";
	}
	
	/**
	 * 分页查询已关闭工单
	 * @author 胡启萌
	 * @date 2015年9月17日
	 */
	@RequestMapping("closed/query.do")
	public @ResponseBody ResultMap closeQuery(CussWorkOrderQueryVO vo) {
		try{
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrderByType(vo, WorkOrderStatus.Closed));		
		}catch(Exception e) {
			logger.error("客户经理分页查询已关闭工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 查询工单
	 */	
	@RequestMapping(value = "/queryCussWorkOrder", method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Page queryCussWorkOrder(HttpServletRequest request,HttpServletResponse response){				
		String temppage=request.getParameter("currentPage");
		Integer pageSize=1;  
		Page page = Page.initPage(temppage,pageSize);
	
		String opType=request.getParameter("opType");
		String workOrderCode=request.getParameter("workOrderCode");
		String workOrderTypeId=request.getParameter("workOrderTypeId");
		String workOrderIssuDesc=request.getParameter("workOrderIssuDesc");
		
		//根据工单类型，找所有的工单子类型
		List<Integer> typelistList=null;		
		if(!CommonUtil.isBlank(workOrderTypeId)){
			typelistList=new ArrayList<Integer>();
			typelistList.add(Integer.valueOf(workOrderTypeId));
			List<Map<String,Object>> cussWorkOrderTypeList=cussWorkOrderService.selectByParentType(Integer.valueOf(workOrderTypeId));
			if(cussWorkOrderTypeList !=null &&cussWorkOrderTypeList.size()>0)
			for(Map<String,Object> temp:cussWorkOrderTypeList){
				typelistList.add(Integer.valueOf(temp.get("typeId").toString()));
			}
		};
				
		List<CussWorkOrder> cussWorkOrderList =null;
		//设置参数
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("serialNumber", workOrderCode);
		paramMap.put("typeId", typelistList);
		paramMap.put("issuDesc", workOrderIssuDesc);
			
		try{	
			if("whole".equals(opType)){//查询所有工单
				String isEvaluate=request.getParameter("isEvaluate");
				String customerEvaluate=request.getParameter("customerEvaluate");
				String approveStatus=request.getParameter("approveStatus");
				paramMap.put("isEvaluate", isEvaluate);
				paramMap.put("customerEvaluate", customerEvaluate);
				paramMap.put("approveStatus", approveStatus);
				cussWorkOrderList =cussWorkOrderService.selectByParam(page,paramMap);								
			}
			if("inhand".equals(opType)){//处理中工单
				paramMap.put("workOrderStatus", WorkOrderStatus.InHand.getValue());
				cussWorkOrderList =cussWorkOrderService.selectByParam(page,paramMap);
			}			
			if("approved".equals(opType)){//已审核工单（包括审核通过、审核未通过两部分）
				paramMap.put("workOrderStatus", 50);
				cussWorkOrderList =cussWorkOrderService.selectByParam(page,paramMap);
			}
			if("audit".equals(opType)){//审核中工单
				paramMap.put("workOrderStatus", WorkOrderStatus.Audit.getValue());
				cussWorkOrderList =cussWorkOrderService.selectByParam(page,paramMap);
			}
			if("closed".equals(opType)){//已关闭工单
				paramMap.put("workOrderStatus", WorkOrderStatus.Closed.getValue());
				cussWorkOrderList =cussWorkOrderService.selectByParam(page,paramMap);
			}			
			page=Page.setValue(page,cussWorkOrderList);
		}catch(Exception e){
			logger.error("客户经理查询工单数据时异常：",e);
		}		 	     
		return page;
	}	
	
	/**
	 * 进入工单处理页
	 */
	@RequestMapping("/intodeal")    
	public ModelAndView intoCussWorkOrderDeal(HttpServletRequest request){
		String workOrderId=request.getParameter("workOrderId");
		CussWorkOrder cussWorkOrder=null;
		if(!LangUtil.isEmpty(workOrderId)){
			//根据工单id，找工单信息						
			try {
				cussWorkOrder=cussWorkOrderService.getCussWorkOrder(Integer.valueOf(workOrderId));
			} catch (Exception e) {
				e.printStackTrace();
			}			
			return new ModelAndView("manager/deal","cussWorkOrder",cussWorkOrder);
		}
		return new ModelAndView("manager/deal");
	}	
	
	/**
	 * 工单处理
	 * @param 
	 */
	@RequestMapping("/deal")
	public @ResponseBody String cussWorkOrderDeal(HttpServletRequest request){
		Map<String,String> paramMap=new HashMap<String,String>();
		paramMap.put("idWorkOrder", request.getParameter("idWorkOrder"));
		paramMap.put("dealType", request.getParameter("dealType"));//通过(approved)、不通过(unapproved)、重新打开(reOpen)
		paramMap.put("feedbackToCustomer", request.getParameter("feedbackToCustomer"));
		try{
			cussWorkOrderService.auditCussWorkOrder(paramMap);
			return StatusEnum.success.getCode();
		}catch(Exception e){
			logger.error("客户经理处理工单，工单处理【阶段："+request.getParameter("dealType")+"】时异常：", e);
			return StatusEnum.fail.getCode();
		}
	}	

	/**
	 * 根据类型查询工单信息
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	private CussWorkOrderQueryVO queryWorkOrderByType(CussWorkOrderQueryVO vo, WorkOrderStatus status){
		switch (status) {
			case Audit:
			case Approved:
				vo.setIsApprove(Constant.ValidFlag.Y.toString());
				//vo.setApprovorUm(user.getUserUMID());
				break;
			default:
				break;
		}
		vo.setWorkOrderStatus(status.getValue());
		return queryWorkOrder(vo);
	}
	
	/**
	 * 查询工单信息
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	private CussWorkOrderQueryVO queryWorkOrder(CussWorkOrderQueryVO vo){
		List<CussWorkOrder> list = cussWorkOrderService.selectListByPage(vo);
		Page.setValues(vo, list);
		return vo;
	}
	
}
